const mongoUtil = require("./mongo_util")
const collectionName = "user"

/* 用户的增删改查 */
function createUser(user, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.insertOne(user, function (err, res) {
            callback(err, res)
        });
    })
}

function getUser(user, callback) {
    console.log("mongoUtil.getCollection=", mongoUtil.getCollection);
    const mmp = mongoUtil.getCollection(collectionName)
    console.log("454545", user);
    console.log("mmp=", mmp);

    mmp.then(collection => {
        collection.find({
            ...user
        }).toArray(function (err, result) { // 返回集合中所有数据
            console.log("getUser result=", result);
            console.log("...user", {
                ...user
            });
            if (result.length) {
                // console.log("gggggg");
                callback(null, result[0])
                new Error()
            } else {
                // console.log("hhhhhhhh");
                callback(err ? err : {
                    reason: "此用户不存在"
                }, result)
            }
        });
    })
}
//获取用户列表
function getUserAll(user, callback) {
    console.log(user);
    const mmp = mongoUtil.getCollection(collectionName)
    mmp.then(collection => {
        collection.find({}).limit(Number(user.pagesize)).skip((Number(user.page) - 1) * Number(user.pagesize)).sort({
            'orderlist.orderNo': -1
        }).toArray(function (err, result) { // 返回集合中所有数据
            if (result.length) {
                // console.log("gggggg");
                callback(null, result)
                new Error()
            } else {
                // console.log("hhhhhhhh");
                callback(err ? err : {
                    reason: "还没有用户"
                }, result)
            }
        });
    })
}

function getuserRegister(user, callback) {
    console.log("mongoUtil.getCollection=", mongoUtil.getCollection);
    const mmp = mongoUtil.getCollection(collectionName)
    console.log("454545", user);
    console.log("mmp=", mmp);

    mmp.then(collection => {
        collection.find({
            ...user
        }).toArray(function (err, result) { // 返回集合中所有数据
            console.log("getUser result=", result);
            console.log("...user", {
                ...user
            });
            if (!result.length) {
                // console.log("gggggg");
                callback(null, result[0])
                new Error()
            } else {
                // console.log("hhhhhhhh");
                callback(err ? err : {
                    reason: "此用户不存在"
                }, result)
            }
        });
    })
}

function putUserInfo(userid, callback) {
    console.log("mongoUtil.getCollection=", mongoUtil.getCollection);
    const mmp = mongoUtil.getCollection(collectionName)
    console.log("mmp=", mmp);

    mmp.then(collection => {
        collection.find({

        }).toArray(function (err, result) { // 返回集合中所有数据
            if (result.length) {
                // console.log("gggggg");
                callback(null, result[0])
                new Error()
            } else {
                // console.log("hhhhhhhh");
                callback(err ? err : {
                    reason: "此用户不存在"
                }, result)
            }
        });
    })
}
module.exports = {
    createUser,
    getUser,
    getuserRegister,
    putUserInfo,
    getUserAll
}